package com.luguz.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.luguz.common.weather.WeatherFilter;
import com.luguz.common.weather.WeatherForecast;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.luguz.common.weather.WeatherInfo;
import java.io.IOException;
import java.util.List;

/**
 * @author Guz
 * @create 2022-02--26 20:28
 */
public class getWeatherInfo {

    //根据城市获取天气信息的java代码
    //cityName 是你要取得天气信息的城市的中文名字，如“北京”，“深圳”
     public static Object getWeatherInform(String city) throws IOException {
         //1.构建客户端
         CloseableHttpClient aDefault = HttpClients.createDefault();
         //2.请求对象
         HttpGet dpGet=new HttpGet("http://wthrcdn.etouch.cn/weather_mini?city="+city);
         //3.发送请求
         CloseableHttpResponse closeableHttpResponse = aDefault.execute(dpGet);
         //输出内容
         //获取对象
         String ent = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
//         System.out.println(ent);
         //解析返回值
         ObjectMapper objectMapper = new ObjectMapper();
         WeatherForecast weatherForecast = objectMapper.readValue(ent, WeatherForecast.class);
         //解析对象的信息 获取城市信息
         if(weatherForecast.getStatus().equals("1002")){
             System.out.println("输入的城市不存在");
             return null;
         }

         return WeatherFilter.FilterWeather(weatherForecast);
    }
}
